package com.iiitb.action;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import co.iiitb.DAO.AdminDAO;
import com.iiitb.model.Client;
import com.iiitb.model.Department;
import com.iiitb.model.Employee;
import com.iiitb.model.Role;
import com.iiitb.model.User_Login;
import com.iiitb.utils.MyLog;
import com.opensymphony.xwork2.ActionSupport;

public class AdminUserFuncActions extends ActionSupport 
{
	private static final long serialVersionUID = 1L;
	
	//will serve as forgien key in client and employee tables
	private int userId;
	
	//Common user parameters for client and employees
	private String username;
	private String pswd;
	private String role;
	
	//parameters for client
	private int clientId;
	private String clientName;
	
	//parameters for employees
	private int empId;
	private int deptId;
	private String empName;
	private int salary;
	
	//allows to differentiate b/w edit and create actions.
	private String actionName;
	
	//data structure for showing table data
	private List<Map<String,Object>> data;
	
	//list of all departments
	private List<Department>departments;
	
	//list of Roles
	private List<Role>roles=new ArrayList<Role>();
	
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPswd() {
		return pswd;
	}
	public void setPswd(String pswd) {
		this.pswd = pswd;
	}
	public String getRole() {
		return role;
	}
	public void setRole(String role) {
		this.role = role;
	}
	public int getClientId() {
		return clientId;
	}
	public void setClientId(int clientId) {
		this.clientId = clientId;
	}
	public String getClientName() {
		return clientName;
	}
	public void setClientName(String clientName) {
		this.clientName = clientName;
	}
	public int getEmpId() {
		return empId;
	}
	public void setEmpId(int empId) {
		this.empId = empId;
	}
	public int getDeptId() {
		return deptId;
	}
	public void setDeptId(int deptId) {
		this.deptId = deptId;
	}
	public String getEmpName() {
		return empName;
	}
	public void setEmpName(String empName) {
		this.empName = empName;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	public String getActionName() {
		return actionName;
	}
	public void setActionName(String actionName) {
		this.actionName = actionName;
	}
	public List<Map<String, Object>> getData() {
		return data;
	}
	public void setData(List<Map<String, Object>> data) {
		this.data = data;
	}
	public List<Department> getDepartments() {
		return departments;
	}
	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}
	public List<Role> getRoles() {
		return roles;
	}
	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}
	
	
	//Pre-processing method for loading create client page.
	public String loadCreateClient() 
	{
		int count;
		setActionName("createClient");
		
		//setting use id and client id
		String maxId= "select max(client_id) from client";
		count=AdminDAO.getMaxId(maxId);
		count=count+1;
		setClientId(count);
		maxId="select max(user_id) from user_login";
		count=AdminDAO.getMaxId(maxId);
		count=count+1;
		setUserId(count);
		
		//setting role to client
		setRole("Client");
		return "loadClient";
	}
	
	public String createClientDB() 
	{
		MyLog.log("In create Client function of Admin actions.");
		int count1, count2;
		String insertSQL;
		
		if (actionName.equals("Create Client")) 
		{
				//Inserting in client table in DB.
				Client clt= new Client();
				clt.setClientId(clientId);
				clt.setClientName(clientName);
				clt.setUserId(userId);
				
				insertSQL= "insert into client "
						+ "(user_id,client_id,client_name) " + 
						"values( "+clt.getUserId()+ " ," + clt.getClientId()+ " ,'" 
						+ clt.getClientName()+ "' );";
				count1=AdminDAO.insert(insertSQL);
				
				//Inserting in user_login table in DB.
				User_Login user=new User_Login();
				user.setUserId(userId);
				user.setPassword(pswd);
				user.setUsername(username);
				user.setRole(role);
				
				insertSQL= "insert into user_login "
						+ "(user_id,username,pswd,role) " + 
						"values( "+user.getUserId()+ ", '" + user.getUsername() + "', '" 
						+ user.getPassword() + "', '" + user.getRole() +"' );";
				count2=AdminDAO.insert(insertSQL);
				
				if(count1!=0 && count2!=0)
				{
					addActionMessage("You have successfully created a Client.");
					return "success";
				}
				else
				{
					addActionError("Error during insertion of client");
					return "fail";
				}
		}
		if (actionName.equals("Edit Client")) 
		{
			//Updating in client table in DB.
			Client clt= new Client();
			clt.setClientId(clientId);
			clt.setClientName(clientName);
			clt.setUserId(userId);
			String updateSQL = "update client "
							+ "set client_name = '" + clt.getClientName()+"' "+
							" where user_id = " +clt.getUserId();
			System.out.println("printing query:"+updateSQL);
			count1=AdminDAO.update(updateSQL);
			
			//Updating in user_login table in DB.
			User_Login user=new User_Login();
			user.setUserId(userId);
			user.setPassword(pswd);
			user.setUsername(username);
			user.setRole(role);
			
			updateSQL= "update user_login "
					+ " set username = ' " + user.getUsername()+ "' , pswd='" + user.getPassword() 
					+ "', role='" + user.getRole() +"' "
					+" where user_id = " +user.getUserId();
			System.out.println("printing query:"+updateSQL);
			count2=AdminDAO.insert(updateSQL);
			if(count1!=0 && count2!=0)
			{
				addActionMessage("You have successfully edited a client.");
				return "success";
			}
			else
			{
				addActionError("Error during updation");
				return "fail";
			}

		}
		addActionError("Error occured please try again");
		return "fail";
	}
	public String showClients() 
	{
		MyLog.log("in show client method of Adim Actions");
		data=new ArrayList<Map<String,Object>>();
		List<Client> clients=AdminDAO.getAllClients();
		List<User_Login>users=AdminDAO.getAllClientUsers();
		Client clt;
		User_Login usr;
		for(int i=0;i<clients.size();i++)
		{
			Map<String,Object> row=new LinkedHashMap<String,Object>();
			clt=clients.get(i);
			usr=users.get(i);
			row.put("client",clt);
			row.put("user",usr);
			data.add(row);
		}
		MyLog.log("returning from showclient() method of Adim Actions");
		return "show";
	}
	
	public String editClient() 
	{
		MyLog.log("in edit client of Admin DAO");
		setActionName("editClient");
		System.out.println("params :" + userId);
		Client clt=AdminDAO.getClientById(userId);
		setClientId(clt.getClientId());
		setClientName(clt.getClientName());
		setRole("client");
		return "showClient";
	}
	public String loadCreateEmployee() 
	{
		int count;
		setActionName("createEmployee");
		
		//setting use id and client id
		String maxId= "select max(emp_id) from employee";
		count=AdminDAO.getMaxId(maxId);
		count=count+1;
		setEmpId(count);
		maxId="select max(user_id) from user_login";
		count=AdminDAO.getMaxId(maxId);
		count=count+1;
		setUserId(count);
		
		//setting list of departments 
		departments=AdminDAO.getAllDepartments();
		
		//setting roles
		Role r1 =new Role(1,"Manager");
		roles.add(r1);
		Role r2= new Role(2,"Team Member");
		roles.add(r2);
		Role r3= new Role(3,"Admin");
		roles.add(r3);
		return "loadEmployee";
	}
	
	public String createEmployeeDB() 
	{
		MyLog.log("In create Employee function of Admin actions.");
		int count1, count2;
		String insertSQL,updateSQL;
		
		if (actionName.equals("Create Employee")) 
		{
				//Inserting in employee table in DB.
				Employee emp=new Employee();
				emp.setUserId(userId);
				emp.setEmployeeId(empId);
				emp.setDeptId(deptId);
				emp.setName(empName);
				emp.setRole(role);
				emp.setSalary(salary);
				insertSQL= "insert into employee "
						+ "(user_id,emp_id,name,dept_id,role,salary) " + 
						"values( "+emp.getUserId()+ " ," + emp.getEmployeeId()+ 
						 " ,'" + emp.getName()+ "' ,"+emp.getDeptId()+" ,'"+
						emp.getRole()+"', "+emp.getSalary()+" );";
				count1=AdminDAO.insert(insertSQL);
				
				//Inserting in user_login table in DB.
				User_Login user=new User_Login();
				user.setUserId(userId);
				user.setPassword(pswd);
				user.setUsername(username);
				user.setRole(role);
				
				insertSQL= "insert into user_login "
						+ "(user_id,username,pswd,role) " + 
						"values( "+user.getUserId()+ ", '" + user.getUsername() + "', '" 
						+ user.getPassword() + "', '" + user.getRole() +"' );";
				count2=AdminDAO.insert(insertSQL);
				
				if(count1!=0 && count2!=0)
				{
					addActionMessage("You have successfully created a Employee.");
					return "success";
				}
				else
				{
					addActionError("Error during creation of Employee");
					return "fail";
				}
		}
		
		if (actionName.equals("Edit Employee")) 
		{
			//Updating in employee table in DB.
			Employee emp=new Employee();
			emp.setUserId(userId);
			emp.setEmployeeId(empId);
			emp.setDeptId(deptId);
			emp.setName(empName);
			emp.setRole(role);
			emp.setSalary(salary);
			updateSQL = "update employee "
							+ "set name = '" + emp.getName()+"' , emp_id=  "+
							emp.getEmployeeId()+", dept_id= "+emp.getDeptId()+
							", role = '"+emp.getRole()+"' ,salary ="+
							emp.getSalary()+"  where user_id = " +emp.getUserId();
			System.out.println("printing query:"+updateSQL);
			count1=AdminDAO.update(updateSQL);
			
			//Updating in user_login table in DB.
			User_Login user=new User_Login();
			user.setUserId(userId);
			user.setPassword(pswd);
			user.setUsername(username);
			user.setRole(role);
			
			updateSQL= "update user_login "
					+ " set username = ' " + user.getUsername()+ "' , pswd='" + user.getPassword() 
					+ "', role='" + user.getRole() +"' "
					+" where user_id = " +user.getUserId();
			System.out.println("printing query:"+updateSQL);
			count2=AdminDAO.insert(updateSQL);
			if(count1!=0 && count2!=0)
			{
				addActionMessage("You have successfully edited a employee.");
				return "success";
			}
			else
			{
				addActionError("Error during updation");
				return "fail";
			}

		}
		
		addActionError("Error occured please try again");
		return "fail";
	}
	
	public String showEmployees() 
	{
		MyLog.log("in show employee method of Adim Actions");
		data=new ArrayList<Map<String,Object>>();
		List<Employee> employees=AdminDAO.getAllEmployees();
		List<User_Login>users=AdminDAO.getAllEmployeeUsers();
		Employee emp;
		User_Login usr;
		for(int i=0;i<employees.size();i++)
		{
			Map<String,Object> row=new LinkedHashMap<String,Object>();
			emp=employees.get(i);
			usr=users.get(i);
			row.put("employee",emp);
			row.put("user",usr);
			data.add(row);
		}
		MyLog.log("returning from showEmployee() method of Adim Actions");
		return "show";
	}
	
	public String editEmployee() 
	{
		MyLog.log("in edit employee of Admin action");
		setActionName("editEmployee");
		System.out.println("params :" + userId);
		Employee emp=AdminDAO.getEmployeeById(userId);
		setEmpId(emp.getEmployeeId());
		setEmpName(emp.getName());
		setSalary(emp.getSalary());
		departments=AdminDAO.getAllDepartments();
		Role r1 =new Role(1,"Manager");
		roles.add(r1);
		Role r2= new Role(2,"Team Member");
		roles.add(r2);
		Role r3= new Role(3,"Admin");
		roles.add(r3);
		return "showEmp";
	}
}
